<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%>
    
	<script type="text/javascript">
	
		//功能树数据源
		var tree_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["id","text","leaf"],
    		url: "tree!function.action"
    	});
		//功能列表数据源
		var data_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["id","name","level","url","remark","parent","state"],
    		url: "func!list.action"
    	});
    	//页面初始化
			
		//定义数据显示面板
		var p1 = {
			id: "p1",
			xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"remark",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			//height:"100%",
			store: data_store,
			headersDisabled: true,			
			columns:[
    	    	{header: "功能编号",		dataIndex: "id", menuDisabled: true},
   				{header: "功能名称",		dataIndex: "name", menuDisabled: true},
   				{header: "URL",			dataIndex: "url", width: 250, menuDisabled: true},
   				{header: "备注",			dataIndex: "remark",	id: "remark", menuDisabled: true}
			],
			buttonAlign: "center",
			bbar: new Ext.ux.PagingToolbar({
   	        	pageSize: limit,
   	        	store: data_store
   	    	}),
			buttons:[{
				text:"新增", 
				handler: function(){
					var panel = this.ownerCt.ownerCt.ownerCt;
					var add = panel.getComponent("p2");
					var node = Ext.getCmp("funcTree").getSelectionModel().getSelectedNode();
					if(node) {
						if(node.isLeaf()) {
							node = node.parentNode;
						}
						Ext.Ajax.request({
							url: "func!queryFuncNo.action",
							success: function(response, opts){
								var d=eval("("+response.responseText+")");
								if(d.code == "1"){
									add.resetParam(node.id,d.data);
									panel.layout.setActiveItem(1);
								} else {
									Ext.Msg.show({
                                        title:"提示",
                                        msg:"操作失败"+d.data+"!",
                                        buttons:Ext.Msg.OK,
                                        icon: Ext.MessageBox.ERROR
                                    });
								}
							}
						});
						oper_type = "add";
						panel.layout.setActiveItem(1);
					}
					else
					{
						Ext.Msg.show({
	                          title:"警告",
	                          msg: "请先选择节点，然后点击新增!",
	                          buttons: Ext.Msg.OK,
	                          animEl: "elId",
	                          icon:Ext.MessageBox.WARNING
                        });
					}
				}
			},{
				text:"修改", 
				handler: function(){
					var p1 = this.ownerCt.ownerCt;
					var panel = p1.ownerCt;
					var record = p1.getSelectionModel().getSelected();
					if (record){
						panel.getComponent("p2").setParam(record.data);
						oper_type = "upd";
						panel.layout.setActiveItem(1);
					}
					else
					{
					  Ext.Msg.show({
	                          title:"警告",
	                          msg: "请先选择需要修改的功能项，然后点击修改!",
	                          buttons: Ext.Msg.OK,
	                          animEl: "elId",
	                          icon:Ext.MessageBox.WARNING
	                          });
					}			
				}
			},{
				text:"删除", 
				handler: function(){
					var delPanel = this.ownerCt.ownerCt;
					var record = delPanel.getSelectionModel().getSelected();
					if(record){
						Ext.Msg.confirm("警告","确定要删除吗?",function(button) {
							if(button == "yes") {
								var url = "func!del.action";
								var params = {
									"func.id":record.data.id
								};
								var params2 = {
									"func.parent": record.data.parent
								};
								oper(url,params,params2,null);
							}
						});
					} else {
						Ext.Msg.show({
	                          title:"警告",
	                          msg: "没有任何行被选中，无法进行删除操作!",
	                          buttons: Ext.Msg.OK,
	                          animEl: "elId",
	                          icon:Ext.MessageBox.WARNING
	                          });
					}
			}}]
		};
		
		var p2 = {
			id: "p2",
		    labelWidth: 60,
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				id: "func.id", 
				fieldLabel: "功能编号",	
				name: "func.id",
				disabled: true,
				anchor: "100%"
			},{
				id: "func.name", 
				fieldLabel: "功能名称",
				allowBlank: false,
				name: "func.name",
				anchor: "100%",
				vtype:"alphanumchinese",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>50){
	        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			},{
				id: "func.url", 	  
				fieldLabel: "URL",
				name: "func.url",
				anchor: "100%",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>200){
	        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		var reg = /.*[\u4e00-\u9fa5]+.*$/;  
	        		if(reg.test(val)){
	        			return "不允许有中文！";
	        		}
	        		return true;
	        	}
			},{
				id: "func.remark", 	  
				fieldLabel: "备注",				
				name: "func.remark",
				anchor: "100%",
				validator:function (val){
	        		if(val=="")return true;
	        		if(Ext.fucCheckCHLength(val)>200){
	        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
	        		}
	        		return true;
	        	}
			},{
				id: "func.parent",
				hidden: true,
				name: "func.parent"
			}],
			buttonAlign: "center",
			buttons:[{text:"保存",id:"save", handler: function(e){
				if(!Ext.getCmp("p2").getForm().isValid()){
					//Ext.Msg.alert("提示","验证不通过！");
						Ext.Msg.show({
	                      title:"提示",
	                      msg:"验证不通过!",
	                      buttons:Ext.Msg.OK,
	                      icon: Ext.MessageBox.ERROR
	                      });
					return;
				}
				
				e.setDisabled(true);
				var form = this.ownerCt.ownerCt.getForm();
				var id = Ext.getCmp("func.id").getValue();
				var name = Ext.getCmp("func.name").getValue();
				var furl = Ext.getCmp("func.url").getValue();
				var remark = Ext.getCmp("func.remark").getValue();
				var parent = Ext.getCmp("func.parent").getValue();
				var url,params;
				if(oper_type == "add") {
					url="func!add.action";
				} else if(oper_type == "upd") {
					url="func!upd.action";
				}
				params = {
					"func.id": id,
					"func.name": name,
					"func.url": furl,
					"func.remark": remark,
					"func.parent": parent,
					"func.state": 1
				};
				params2 = {
					"func.parent": parent
				};
				oper(url,params,params2,form.ownerCt);
			}},{text:"取消", handler: function(){
				this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
			}
		}],
		setParam: function(data){
			var p2 = this;
			p2.getComponent("func.id").setValue(data.id);
			p2.getComponent("func.name").setValue(data.name);
			p2.getComponent("func.url").setValue(data.url);
			p2.getComponent("func.remark").setValue(data.remark);
			p2.getComponent("func.parent").setValue(data.parent);
		},
		resetParam: function(parent, funcno){
			var p2 = this;
			p2.getComponent("func.id").setValue(funcno);
			p2.getComponent("func.name").setValue("");
			p2.getComponent("func.url").setValue("");
			p2.getComponent("func.remark").setValue("");
			p2.getComponent("func.parent").setValue(parent);
		}
	};

		
	var fun_pel = new Ext.Panel({
		id: "panel",
		border : false, 
		layout: "border",
		items: [{
			id:"funcTree",
			xtype:"billTreePanel",
			dataUrl:"tree!function.action",
			autoScroll: true,
			root: new Ext.tree.AsyncTreeNode({
				id: "root",
				expanded: true
			}),
			listeners: {
				"click": function(node){
					tree_store.load({params:{node:node.attributes.id}});
					data_store.baseParams = data_store.baseParams || {};
					if(node.isLeaf()) {
						data_store.baseParams["func.id"] = node.attributes.id;
						delete data_store.baseParams["func.parent"];
					} else {
						delete data_store.baseParams["func.id"];
						data_store.baseParams["func.parent"] = node.attributes.id;
					}
					data_store.load({params:{start : 0,limit : limit}});
					Ext.getCmp('mycontainer').layout.setActiveItem(0);
				}
			}},{
				id: "mycontainer",
				region: "center",
				border : false, 
				layout:"card",
				activeItem:0,//显示第一个容器
				items: [p1,p2]
			}]
	});	
	
	
	var oper = function(url, params, parent, panel){
		Ext.Ajax.request({
			clientValidation: true,
			params: params,
			url: url,
			success: function(response, opts){
				var d=eval("("+response.responseText+")");
				if(d.code == "1"){
					var node = Ext.getCmp("funcTree").getSelectionModel().getSelectedNode();
					if(node) {
						if(node.isLeaf()) {
							node = node.parentNode;
						}
						node.reload();
						node.select();
					};
					if(parent != null) {
						Ext.getCmp("p1").getBottomToolbar().doRefresh();
					}
                    Ext.Msg.show({
	                    title:"提示",
	                    msg:"操作成功!",
	                    buttons:Ext.Msg.OK,
	                    icon: Ext.MessageBox.INFO
	                });
	                					
					if(panel != null)
						panel.layout.setActiveItem(0);
				} else {
						Ext.Msg.show({
	                        title:"提示",
	                        msg:"操作失败！",
	                        buttons:Ext.Msg.OK,
	                        icon: Ext.MessageBox.ERROR
	                      });
				}
				Ext.getCmp("save").setDisabled(false);
			}
		});
	};
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(fun_pel);
	CenterPanel.doLayout();
	</script>
    
